Public Class CombatLogFile
    Private m_buffer() As String
    Private ErrHdlr As New ErrorHandler

    Private mYear As Integer = Now.Year

    Private ReplaceFrom() As String = {"(Not Flying, 50 yd)"}
    Private ReplaceTo() As String = {"(Not Flying 50 yd)"}

    Public ReadOnly Property Length() As Integer
        Get
            If m_buffer Is Nothing Then
                Return 0
            Else
                Return m_buffer.Length
            End If
        End Get
    End Property

    Public ReadOnly Property CombatLog() As String()
        Get
            Return m_buffer
        End Get
    End Property

    Public Function Items(ByVal Index As Long) As String()
        If Index < 0 Or m_buffer.Length <= Index Then
            Return Nothing
        End If

        Try
            Dim bfr() As String
            If m_buffer(Index) = "" Then
                Return Nothing
            Else
                Dim rep As Integer
                Dim replaced As Boolean = False
                For rep = 0 To ReplaceFrom.Length - 1
                    If InStr(m_buffer(Index), ReplaceFrom(rep)) > 0 Then
                        m_buffer(Index) = Replace(m_buffer(Index), ReplaceFrom(rep), ReplaceTo(rep))    ' --- replace when field value includes comma
                        replaced = True
                    End If
                Next
                bfr = Split(Replace(m_buffer(Index), "  ", ","), ",")

                Dim i As Integer
                For i = 0 To bfr.Length - 1
                    If replaced Then    ' --- replace back comma
                        For rep = 0 To ReplaceFrom.Length - 1
                            bfr(i) = Replace(bfr(i), ReplaceTo(rep), ReplaceFrom(rep))
                        Next
                    End If
                    If Microsoft.VisualBasic.Left(bfr(i), 1) = """" And Microsoft.VisualBasic.Right(bfr(i), 1) = """" Then ' --- remove double quate from string
                        bfr(i) = Microsoft.VisualBasic.Mid(bfr(i), 2, Len(bfr(i)) - 2)
                    End If
                Next

                Return bfr
            End If
        Catch ex As Exception
            ErrHdlr.ErrorMsgBox(ex)
            Return Nothing
        End Try

    End Function

    Public Sub ReplaceCombatLog(ByVal Index As Long, ByVal Text As String)
        If Index >= 0 And Index < m_buffer.Length Then
            m_buffer(Index) = Text
        End If
    End Sub

    Public Function EventTime(ByVal Index As Long) As DateTime
        Dim bfr As String
        Dim evetime As DateTime

        If Index < 0 Or m_buffer.Length <= Index Then
            Return Nothing
        End If

        Try
            bfr = m_buffer(Index)
            If InStr(bfr, "  ") = 0 Then
                Return Nothing
            Else
                bfr = Mid(bfr, 1, InStr(bfr, "  ") - 1)
                evetime = mYear.ToString & "/" & bfr
                Return evetime
            End If
        Catch ex As Exception
            ErrHdlr.ErrorMsgBox(ex)
            Return Nothing
        End Try
    End Function

    Public Function ReadLogFile(ByVal Filename As String) As Boolean
        Dim FSOlog As System.IO.FileInfo
        FSOlog = My.Computer.FileSystem.GetFileInfo(Filename)

        If FSOlog.Exists Then
            mYear = FSOlog.LastWriteTime.Year
            If mYear < 2000 Then mYear = Now.Year

            Try
                m_buffer = System.IO.File.ReadAllLines(Filename, System.Text.Encoding.UTF8)
            Catch ex As Exception
                ErrHdlr.ErrorMsgBox(ex, "Cannot read big combat log file")
                Return False
            End Try
        Else
            Return False
        End If

        Return True
    End Function

    Public Function WriteLogFile(ByVal Filename As String) As Boolean
        Try
            System.IO.File.WriteAllLines(Filename, m_buffer, System.Text.Encoding.UTF8)
            'FileOpen(1, Filename, OpenMode.Output, OpenAccess.Write, OpenShare.Default)
            'Dim i As Long
            'For i = 0 To m_buffer.Length - 1
            '    If m_buffer(i) <> "" Then
            '        Print(1, m_buffer(i) & vbCrLf)
            '    End If
            'Next
            'FileClose(1)
        Catch ex As Exception
            'FileClose(1)
            ErrHdlr.ErrorMsgBox(ex)
            Return False
        End Try
    End Function
End Class
